package com.das.mechanic_base.utils;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.TimeInterpolator;
import android.view.View;
import android.view.ViewGroup;

public class X3AnimationUtils {


    /**
     * @param target
     * @param star     动画起始坐标
     * @param end      动画终止坐标
     * @param duration 持续时间
     * @return 创建一个从左到右的飞入动画
     */
    public static ObjectAnimator createFadesInFromLtoR(final View target, float star, float end,
                                                       int duration, TimeInterpolator interpolator) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(target, "translationX",
                star, end);
        animator.setInterpolator(interpolator);
        animator.setDuration(duration);
        return animator;
    }


    /**
     * @param target
     * @param star     动画起始坐标
     * @param end      动画终止坐标
     * @param duration 持续时间
     * @param startDelay 延迟时间
     * @return 向上飞 淡出
     */
    public static ObjectAnimator createFadesOutAnimator(final View target, float star, float end,
                                                        int duration, int startDelay) {
        PropertyValuesHolder translationY = PropertyValuesHolder.ofFloat("translationY", star, end);
        PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f, 0f);
        ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(target, translationY, alpha);
        animator.setStartDelay(startDelay);
        animator.setDuration(duration);
        return animator;
    }

    public static AnimatorSet startAnimation(ObjectAnimator animatorFirst, ObjectAnimator animatorSecond) {
        AnimatorSet animSet = new AnimatorSet();
        animSet.play(animatorFirst).before(animatorSecond);
        animSet.start();
        return animSet;
    }

    /**
     * @param target
     * @return 向上飞 淡出
     */
    public static ObjectAnimator scaleXY(final View target) {
        PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0f, 1f);
        PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0f, 1f);
        ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(target, scaleX, scaleY);
        animator.setDuration(300);
        return animator;
    }

    /**
     * @param target
     * @return 向上飞 淡出
     */
    public static ObjectAnimator scaleCloseXY(final View target) {
        PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1f, 0f);
        PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1f, 0f);
        ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(target, scaleX, scaleY);
        animator.setDuration(300);
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                if (target != null) {
                    ViewGroup parent = (ViewGroup) target.getParent();
                    if (parent != null) {
                        parent.removeView(target);
                    }
                }
            }
        });
        return animator;
    }

    /**
     * @param target
     * @return 闪烁
     */
    public static ObjectAnimator alphaXY(final View target,int time) {
        PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0f,1f, 0f);
        ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(target, alpha);
        animator.setDuration(time);
        return animator;
    }

}
